package org.ilc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * 系统配置
 * @author xumin
 *
 */
public class Config {
	private Properties prop;

    /**
     *
     * @param path
     * @throws java.io.FileNotFoundException
     * @throws java.io.IOException
     */
	private Config(String path) throws FileNotFoundException, IOException {
		prop = new Properties();

		File file = new File(path);
		prop.load(new FileInputStream(file));
	}

    /**
     *
     * @param path
     * @return
     * @throws java.io.FileNotFoundException
     * @throws java.io.IOException
     */
	public static Config load(String path) throws FileNotFoundException,
			IOException {
		return new Config(path);
	}

    /**
     *
     * @param key
     * @return
     */
	public String get(String key) {
		if (key == null || "".equals (key.trim ()))
			return null;
		String value = prop.getProperty(key);
		return null == value ? value : value.trim();

	}

    /**
     *
     * @param key
     * @return
     */
	public boolean getBoolean(String key) {
        return getBoolean (key, false);
	}

    /**
     *
     * @param key
     * @return
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        boolean value = defaultValue;
        try {
            value = Boolean.parseBoolean(prop.getProperty(key));
        } catch (Exception e) {
            value = defaultValue;
        }
        return value;
    }

    /**
     *
     * @param key
     * @param defaultValue
     * @return
     */
	public int getInt(String key, int defaultValue) {
		if (key == null)
			return 0;
        int value = defaultValue;
		try {
			value = Integer.parseInt(prop.getProperty(key));
		} catch (Exception e) {
            value = defaultValue;
		}
		return value;
	}

    /**
     * 清除所有配置信息
     */
	public void cleanUp(){
		prop.clear();
		prop=null;
	}
}
